package com.wl.employee.controller;

import com.wl.employee.entity.Employee;
import com.wl.employee.service.EmployeeService;
import com.wl.employee.vo.EmployeeVO;
import com.wl.employee.vo.ViewVO;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class EmployeeController {
    private static Logger log = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/jsonType/addEmployee.do", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded"})
    @ResponseBody
    public ViewVO<Employee> addEmployee(Employee employee) {
        ViewVO vo = new ViewVO(0, "ok");
        this.employeeService.addEmployee(employee);

        return vo;
    }

    @RequestMapping(value = "jsonType/editEmployee.do", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded"})
    @ResponseBody
    public ViewVO<Employee> editEmployee(Employee employee) {
        ViewVO vo = new ViewVO(0, "ok");
        employeeService.editEmployee(employee);
        return vo;
    }

    @RequestMapping(value = "get.do", method = {RequestMethod.GET})
    @ResponseBody
    public ViewVO<Employee> getEmployee(@RequestParam(name = "id") Integer id) {
        ViewVO vo = new ViewVO(0, "ok");

        Employee employee = employeeService.findById(id);

        EmployeeVO employeeVO = new EmployeeVO();
        BeanUtils.copyProperties(employee, employeeVO);
        employeeVO.setSexStr(0 == employee.getSex() ? "男" : "女");
        employeeVO.setBirthday(DateFormatUtils.format(employee.getBirthday(), "yyyy-MM-dd"));
        employeeVO.setJoinDate(DateFormatUtils.format(employee.getJoinDate(), "yyyy-MM-dd"));
        vo.setData(employeeVO);
        return vo;
    }

    @RequestMapping(value = "del.do", method = {RequestMethod.GET})
    @ResponseBody
    public ViewVO<Employee> delEmployee(Employee employee) {
        ViewVO vo = new ViewVO(0, "ok");
//        Employee employee = new Employee();
//        employee.setId(id);
        this.employeeService.deleteEmployee(employee);
        return vo;
    }

    @RequestMapping(value = "/list.do", method = {RequestMethod.GET}, consumes = {"application/x-www-form-urlencoded"})
    @ResponseBody
    public ViewVO<Employee> findAll() {
        ViewVO vo = new ViewVO(0, "ok");

        List<Employee> list = this.employeeService.findAll();

        List<EmployeeVO> listVO = new ArrayList<>();
        EmployeeVO employeeVO;
        for (int i = 0; i < list.size(); i++) {
            Employee employee = list.get(i);
            employeeVO = new EmployeeVO();
            employeeVO.setId(employee.getId());
            employeeVO.setEname(employee.getEname());
            employeeVO.setSexStr(0 == employee.getSex() ? "男" : "女");
            employeeVO.setBirthday(DateFormatUtils.format(employee.getBirthday(), "yyyy-MM-dd"));
            employeeVO.setJoinDate(DateFormatUtils.format(employee.getJoinDate(), "yyyy-MM-dd"));
            employeeVO.setEno(employee.getEno());
            listVO.add(employeeVO);
        }

        vo.setData(listVO);

        return vo;
    }

    public static void main(String[] args) {
        System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
    }

}